background_image_name = "background.jpg"

import pygame
from pygame.locals import *
from sys import exit
import player
import camera

if __name__ == '__main__':
    pygame.init()

    SCREEN_ORIGIN_SIZE = (1024, 640)
    SCREEN_SIZE = SCREEN_ORIGIN_SIZE

    scale = (1, 1)
    screen = pygame.display.set_mode(SCREEN_SIZE, DOUBLEBUF | RESIZABLE, 32)

    background_image = pygame.image.load(background_image_name).convert()
    background_image_size = background_image.get_size()
    background_image = pygame.transform.scale(background_image, SCREEN_SIZE)

    objectGroup = pygame.sprite.Group()

    init_pos = (110, 90)
    ball = player.Player(init_pos, r=10)
    g = 0.01
    u = 2.5
    ball.add_f("G", (0, ball.m * g))
    ball.set_u(u)

    cam = camera.Camera(screen, scale, SCREEN_ORIGIN_SIZE)

    ball.off_ground()

    objectGroup.add(ball)

    pygame.key.set_repeat(5)

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            if event.type == RESIZABLE:
                SCREEN_SIZE = event.size
                scale = (SCREEN_SIZE[0] / SCREEN_ORIGIN_SIZE[0], SCREEN_SIZE[1] / SCREEN_ORIGIN_SIZE[1])
                cam.set_scale(scale)
                screen = pygame.display.set_mode(SCREEN_SIZE, DOUBLEBUF | RESIZABLE, 32)
                background_image = pygame.transform.scale(background_image, SCREEN_SIZE)
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    exit()
        keys = pygame.key.get_pressed()

        if keys[K_LEFT] or keys[K_a]:
                ball.hori_move(-1)
        elif keys[K_RIGHT] or keys[K_d]:
                ball.hori_move(1)
        elif keys[K_UP] or keys[K_w]:
                ball.jump()
        elif keys[K_q] or keys[K_p]:
           if ball.cd == 0:
                (bullet_pos, bullet_direc, bullet_type) = ball.emit_bullet()
                bullet_pos = (bullet_pos[0], bullet_pos[1])
                bullet = player.Bullet(bullet_pos, type = bullet_type)
                objectGroup.add(bullet)
        elif keys[K_1]:
                ball.amor_change("stone")
        elif keys[K_2]:
                ball.amor_change("gun")
        elif keys[K_3]:
                ball.amor_change("autogun")
        elif keys[K_4]:
                ball.amor_change("grenade")

        pressed_mouse = pygame.mouse.get_pressed()

        if pressed_mouse[0]:
            if ball.cd == 0:
                (bullet_pos, bullet_direc, bullet_type) = ball.emit_bullet()
                mouse_pos = pygame.mouse.get_pos()
                mouse_pos = (mouse_pos[0] + cam.topleft[0], mouse_pos[1] + cam.topleft[1])
                angle = ball.get_emit_direc(mouse_pos)
                bullet = player.Bullet(bullet_pos, angle = angle, type = bullet_type)
                objectGroup.add(bullet)

        elif pressed_mouse[1] or pressed_mouse[2]:
            ball.rotate_amor()

        # screen.blit(background_image, (0, 0))
        cam.screen_background_blit(background_image)
        for obj in objectGroup:
            obj.update(pygame.time.get_ticks())
            # screen.blit(obj.image, obj.rect)
            cam.screen_blit(obj.image, obj.rect)
            if obj._type == "player0":
                cam.camera_move(obj.rect, ball._direc)
                if obj.rect.x < 0:
                    obj.rect.x = 0
                    cam.topleft = (0, 0)
            if obj.rect.y > 400 * scale[1]:
                if obj._type == "player0":
                    obj.on_ground()
                    obj.rect.y = 400 * scale[1]
                else:
                    obj.explode(objectGroup)
                    objectGroup.remove(obj)
            if not cam.in_camera(obj.rect):
                objectGroup.remove(obj)

        pygame.display.flip()     #better double-flush



